(function( $ )
{  	
  $.fn.TwifySlider = function( method ) {
    
	var anim = {
		outPos		: 1000,
		inPos		: 0,
		inDuration	: 100,
		outDuration	: 300,
		outOpac		: 0,
		inOpac		: 1
	};
	var currentSlide = 0;
	var slideCount = 0;
	
	return this.each(function(index, value){
				
				// Add setpage listener to window
				var slider = $(value);
				var prevbtn = slider.find(".prev");
				var nextbtn = slider.find(".next");
				var slides = slider.find(".item");
				
				prevbtn.css({cursor:"pointer"}).click(showPrevSlide);
				nextbtn.css({cursor:"pointer"}).click(showNextSlide);
				
				slideCount = slides.length;
				
				for(var i =0;i<slideCount;i++)
				{
					if(i != currentSlide)
					{
						$(slides[i]).css({left:anim.outPos});
					}
				}
				
				showSlide(currentSlide);				
				
				function showPrevSlide()
				{
					if(currentSlide == 0) showSlide(slideCount-1);
					else showSlide(currentSlide-1);
				}
				function showNextSlide()
				{
					if(currentSlide == slideCount-1) showSlide(0);
					else showSlide(currentSlide+1);
				}
				function showSlide(sindex)
				{
					slideOut($(slides[currentSlide]));
					slideIn($(slides[sindex]));
					
					currentSlide = sindex;
				}
				function slideOut(el,cb)
				{
					console.log("Moving out slide "+ currentSlide);
					$(el).animate(
					{
						left:anim.outPos, 
						opacity: anim.outOpac
					}, {duration: anim.inDuration, complete:cb||function(){}, easing: "swing"}); 
				}
				function slideIn(el,cb)
				{
					console.log("Moving in next slide");
					$(el).animate(
					{
						left:anim.inPos, 
						opacity: anim.inOpac
					}, {duration: anim.inDuration, complete:cb||function(){}, easing: "swing"});
				}
		});
  };
  

})( jQuery );
